package com.mew.lobby.handler.user;

import com.alibaba.nacos.common.utils.Md5Utils;
import com.mew.common.dc.redis.UserProfileRedisService;
import com.mew.common.entity.user.User;
import com.mew.common.exception.ResponseCode;
import com.mew.common.rest.bo.MewSession;
import com.mew.common.util.MD5Encrypt;
import com.mew.common.util.StringUtils;
import com.mew.lobby.annotation.LobbyHandler;
import com.mew.lobby.message.request.EditResponseMessage;
import com.mew.lobby.net.LobbyHandlerMapCmdType;
import com.mew.lobby.net.MewHandler;
import com.mew.lobby.provide.UserFeignService;
import com.zebra.remoting.protocol.ZebraCommand;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**修改头像
 * @author dylin
 * @create 2021/1/26 0026
 */
@Service
@Slf4j
@LobbyHandler(LobbyHandlerMapCmdType.PasswordEdit)
public class PasswordEditHandler implements MewHandler<EditResponseMessage> {
    @Autowired
    private UserProfileRedisService userProfileRedisService;
    @Autowired
    private UserFeignService userFeignService;

    @Override
    public void handle(ZebraCommand<EditResponseMessage> zebraCommand, MewSession session) {
        long userId = zebraCommand.getUserId();
        String oldS = zebraCommand.getRequest().getOldS();
        String newS = zebraCommand.getRequest().getNewS();
        User user = userProfileRedisService.getRedisUserById(userId);
        if(null == user){
            zebraCommand.sendFailureCode(ResponseCode.LoginUn.code());
            return;
        }
        if(!user.getPassword().equals(MD5Encrypt.MD5(oldS)) || StringUtils.isEmpty(newS)){
            zebraCommand.sendFailureCode(ResponseCode.ParamInvalid.code());
            return;
        }
        int code = userFeignService.updateUser(
                User
                        .builder()
                        .id(userId)
                        .password(MD5Encrypt.MD5(newS))
                        .build()
        );
        zebraCommand.sendFailureCode(code);
    }
}
